import React, { FC, useEffect } from 'react'
import { Form, Input, Checkbox } from 'antd'
import { QuestionParagraphPropsType } from './interface'
const { TextArea } = Input

const PropComponent: FC<QuestionParagraphPropsType> = (props: QuestionParagraphPropsType) => {
    const { text, isCenter, onChange, disabled } = props
    const [form] = Form.useForm()

    // 表单手动更新事件
    const handleOnValuesChange = () => {
        const newProps = form.getFieldsValue()
        if (onChange) {
            onChange(newProps)
        }
    }
    // 渲染对比：当title更新时，更新form数据
    useEffect(() => {
        form.setFieldsValue({
            text,
            isCenter,
        })
    }, [text, isCenter])

    return (
        <Form
            layout="vertical"
            onValuesChange={handleOnValuesChange}
            initialValues={{ text, isCenter }}
            form={form}
            disabled={disabled}
        >
            <Form.Item
                label="段落内容"
                name="text"
                rules={[{ required: true, message: '请输入段落内容' }]}
            >
                <TextArea />
            </Form.Item>
            <Form.Item name="isCenter" valuePropName="checked">
                <Checkbox>居中显示</Checkbox>
            </Form.Item>
        </Form>
    )
}
export default PropComponent
